﻿using System;
using System.Data;
using System.Globalization;
using System.Web.UI;
using System.Xml;
using BusinessLogic.ClassFactory;
using Common;
using DataAccess;
using Util.PageInfo;
using ValidateCommon;
using BusinessLogic.BusinessRules;
using System.Text;
using System.IO;

namespace RichECMS.ServerPages.Article
{
    public partial class Update : Article.Base.ArticleSave
    {
        public int intCurrentSectionID;
        protected void Page_Load(object sender, EventArgs e)
        {
            Hiddenpathmain.Value = "../../news/" + State.LoginUser.VartualPath + "/Pcfiles/";
            int intSectionId = 0;
            if (!IsPostBack)
            {
                if (Request["Section"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Section"].ToString().Trim()))
                    {
                        State.intSTID = Convert.ToInt32(Request["Section"].ToString());
                        aBack.HRef += "?Section=" + Request["Section"];
                        BusinessLogic.BusinessRules.Sections.FindSectionReferRoot(State.intSTID, ref intSectionId);
                        State.intSTID = intSectionId;
                        intCurrentSectionID = intSectionId;
                    }
                    else
                    {
                        Alert("参数错误");
                        Response.End();
                    }
                }
                if (Request["Id"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Id"].ToString().Trim()))
                    {
                        using (SectionsFactory objFactory = new SectionsFactory())
                        {
                            BusinessLogic.BusinessRules.Sections objSections = objFactory.Creator(State.intSTID);
                            if (objSections != null)
                            {
                                DataTable tblTmp = objSections.Data.Tables[0];
                                DataRow row = tblTmp.Rows[0];
                                if (row["chvSTCName"] != DBNull.Value)
                                {
                                    LitPageName.Text = objSections.DataItem.chvSTCName;
                                    classChooser.Items.Add(objSections.DataItem.chvSTCName);
                                }
                                if (row["chvDataModel"] != DBNull.Value)
                                {
                                    SyncDataLabel(objSections.DataItem.chvDataLabel);
                                }
                                if (row["chvDataModel"] != DBNull.Value)
                                {
                                    LitExtention.Text = ShowExtension(objSections.DataItem.chvDataModel);
                                }
                            }
                        }
                        InitDataBind(Convert.ToInt32(Request["Id"].ToString()), intSectionId);
                        LitPaagePath.Text = PageInfo.ShowPagePath(State.intSTID);
                    }
                    else
                    {
                        Alert("参数错误");
                        Response.End();
                    }
                }
                else
                {
                    Alert("参数错误");
                    Response.End();
                }
            }
        }

        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="strXML"></param>
        private void SyncDataLabel(string strXML)
        {
            try
            {
                if (strXML.Trim() == "")
                {
                    return;
                }

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(strXML);
                //查找标签
                XmlNode nodeLabel = xmldoc.SelectSingleNode("//infotitle");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        Littitle.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoclass");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitInfoClass.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infosubtitle");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitSubTitle.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoArticleMan");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitInfoArticleMan.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoimage");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitInfoImage.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infodescription");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitInfoDescription.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infosummary");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitInfoSummary.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infocontent");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LitInfoContent.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infosource");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralSource.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoauthor");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralAuthor.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoalltop");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralAllTop.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infosectiontop");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralSectonTop.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoalltj");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralAllTJ.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infosectiontj");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralSectionTJ.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoscrollnews");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralScrollNews.Text = nodeLabel.InnerXml;
                    }
                }
                nodeLabel = xmldoc.SelectSingleNode("//infoverify");
                if (nodeLabel != null)
                {
                    if (nodeLabel.InnerXml.Trim() != "")
                    {
                        LiteralVerify.Text = nodeLabel.InnerXml;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message.Replace("\r\n", ""));
            }
        }

        protected void ExpandReset()
        {
            //异常情况下恢复自定义扩展中填充的数据
            using (SectionsFactory objFactory = new SectionsFactory())
            {
                BusinessLogic.BusinessRules.Sections objSections = objFactory.Creator(State.intSTID);
                if (objSections != null)
                {
                    DataTable tblTmp = objSections.Data.Tables[0];
                    DataRow row = tblTmp.Rows[0];
                    if (row["chvDataModel"] != DBNull.Value)
                    {
                        StringBuilder objbuilder = new StringBuilder();
                        try
                        {
                            if (!row["chvDataModel"].ToString().Trim().Equals(""))
                            {
                                XmlDocument strXML = new XmlDocument();
                                strXML.LoadXml(row["chvDataModel"].ToString());
                                XmlNode objExtenRootNode = strXML.SelectSingleNode("/datamodel");
                                if (objExtenRootNode != null)
                                {
                                    if (objExtenRootNode.HasChildNodes)
                                    {
                                        XmlNodeList objXMLNOdeList = objExtenRootNode.ChildNodes;
                                        foreach (XmlNode objNodeTemp in objXMLNOdeList)
                                        {
                                            if (objNodeTemp.Attributes["type"] != null)
                                            {
                                                string strType = objNodeTemp.Attributes["type"].Value;
                                                if (File.Exists(Server.MapPath("~/") + "ServerPages\\ExpandEditor\\" + strType + "\\template.htm"))
                                                {
                                                    using (FileStream fs = new FileStream(Server.MapPath("~/") + "ServerPages\\ExpandEditor\\" + strType + "\\template.htm", FileMode.Open, FileAccess.Read, FileShare.Read))
                                                    {
                                                        StreamReader sr = new StreamReader(fs);
                                                        string temp = sr.ReadToEnd();
                                                        temp = temp.Replace("$TitlePosition$", objNodeTemp.Attributes["description"].Value).Replace("$NamePosition$", objNodeTemp.Name).Replace("$IDPosition$", objNodeTemp.Name).Replace("$CurrentValue$", Request[objNodeTemp.Name]);
                                                        objbuilder.Append(temp);
                                                        sr.Close();
                                                    }
                                                }
                                                else
                                                {
                                                    Alert(string.Format("扩展配置异常:栏目{0}中的节点ID为{1}的type属性为{2}所对应的文件不存在", State.intSTID, objNodeTemp.Name, strType));
                                                    return;
                                                }
                                            }
                                            else
                                            {
                                                Alert(string.Format("扩展配置异常:栏目{0}中的节点ID为{1}的type属性不存在", State.intSTID, objNodeTemp.Name));
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Alert(ex.Message.Replace("\r\n", ""));
                        }
                        LitExtention.Text = "";
                        LitExtention.Text = objbuilder.ToString();
                    }
                }
            }
        }

        /// <summary>
        /// 展示数据
        /// </summary>
        /// <param name="intArticleId">文章ID</param>
        private void InitDataBind(int intArtRecId, int intSectionId)
        {
            using (DBManager objDBManager = new DBManager())
            {
                objDBManager.BeginTransaction();
                try
                {
                    //关联表
                    ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory();
                    BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objArtSecRelationFactory.Creator(intArtRecId);
                    if (objArtSecRelation != null)
                    {
                        DataTable tblTmp = objArtSecRelation.Data.Tables[0];
                        DataRow row = tblTmp.Rows[0];
                        this.txtOrder.Value = row["intSort"] == DBNull.Value ? "0" : objArtSecRelation.DataItem.intSort.ToString(CultureInfo.InvariantCulture);
                        if (row["intVerify"] != DBNull.Value)
                        {
                            if (objArtSecRelation.DataItem.intVerify == 1)
                            {
                                CkboxPublish.Checked = true;
                            }
                        }
                        if (row["intGlobalRec"] != DBNull.Value)
                        {
                            if (objArtSecRelation.DataItem.intGlobalRec == 1)
                            {
                                CkboxIsGlobalRec.Checked = true;
                            }
                        }
                        if (row["intSectionRec"] != DBNull.Value)
                        {
                            if (objArtSecRelation.DataItem.intSectionRec == 1)
                            {
                                CkboxIsPartRec.Checked = true;
                            }
                        }
                        if (row["intGlobalTop"] != DBNull.Value)
                        {
                            if (objArtSecRelation.DataItem.intGlobalTop == 1)
                            {
                                CkboxIsGlobalTop.Checked = true;
                            }
                        }
                        if (row["intSectionTop"] != DBNull.Value)
                        {
                            if (objArtSecRelation.DataItem.intSectionTop == 1)
                            {
                                CkboxIsPartTop.Checked = true;
                            }
                        }
                        if (row["intScroll"] != DBNull.Value)
                        {
                            if (objArtSecRelation.DataItem.intScroll == 1)
                            {
                                CkboxIsScroll.Checked = true;
                            }
                        }
                        this.TextReadCount.Value =objArtSecRelation.DataItem.intAccessCount.ToString();
                    }
                    //文章表
                    ArticleFactory objFactory = new ArticleFactory();
                    BusinessLogic.BusinessRules.Article objArticle = objFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                    if (objArticle != null)
                    {
                        DataTable tblTmp = objArticle.Data.Tables[0];
                        DataRow row = tblTmp.Rows[0];
                        if (row["chvKeyWords"] != DBNull.Value)
                        {
                            string[] keyWord = objArticle.DataItem.chvKeyWords.Split(',');
                            if (keyWord.Length == 4)
                            {
                                this.txtkeyWord1.Value = keyWord[0];
                                this.txtkeyWord2.Value = keyWord[1];
                                this.txtkeyWord3.Value = keyWord[2];
                                this.txtkeyWord4.Value = keyWord[3];
                            }
                        }
                        if (row["chvMainPic"] != DBNull.Value)
                        {
                            this.ImgShow.Src = objArticle.DataItem.chvMainPic.Trim().Length == 0
                                                   ? Hiddenpathmain.Value + "default.jpg"
                                                   : Hiddenpathmain.Value + objArticle.DataItem.chvMainMiniPic;
                        }
                        else
                        {
                            this.ImgShow.Src = Hiddenpathmain.Value + "default.jpg";
                            Imghidden.Value = "";
                        }
                        this.txtSummary.Value = row["chvSummary"] == DBNull.Value ? "" : objArticle.DataItem.chvSummary.Replace("\"../Pcfiles/", "\"/news/" + State.LoginUser.VartualPath + "/Pcfiles/"); ;
                        this.txtContent.Value = row["txtContent"] == DBNull.Value ? "" : objArticle.DataItem.txtContent.Replace("\"../Pcfiles/", "\"/news/" + State.LoginUser.VartualPath + "/Pcfiles/");
                        this.txtIssueTime.Value = row["dtmInTime"] == DBNull.Value ? "" : objArticle.DataItem.dtmInTime.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                        this.txtTitle.Value = row["chvTitle"] == DBNull.Value ? "" : objArticle.DataItem.chvTitle;
                        this.txtSecondTitle.Value = row["chvSubTitle"] == DBNull.Value ? "" : objArticle.DataItem.chvSubTitle;
                        this.txtAuthor.Value = row["chvArticleInMan"] == DBNull.Value ? "" : objArticle.DataItem.chvArticleInMan;
                        this.txtdescription.Value = row["chvDescription"] == DBNull.Value ? "" : objArticle.DataItem.chvDescription;
                        this.TextRedirectUrl.Value = row["chvRedirectUrl"] == DBNull.Value ? "" : objArticle.DataItem.chvRedirectUrl;
                        this.txtvedio.Value = row["chvVedioPath"] == DBNull.Value ? "" : objArticle.DataItem.chvVedioPath;
                        this.txtfile.Value = row["chvFilePath"] == DBNull.Value ? "" : objArticle.DataItem.chvFilePath;
                        this.txtseotitle.Value = row["txtSeoTitle"] == DBNull.Value ? "" : objArticle.DataItem.txtSeoTitle;
                        this.txtseokeywords.Value = row["txtSeoKeywords"] == DBNull.Value ? "" : objArticle.DataItem.txtSeoKeywords;
                        this.txtseodescription.Value = row["txtSeodescription"] == DBNull.Value ? "" : objArticle.DataItem.txtSeodescription;
                        this.TextSource.Value = row["chvArticleSource"] == DBNull.Value ? "" : objArticle.DataItem.chvArticleSource;
                        this.TextAuthor.Value = row["chvAuthor"] == DBNull.Value ? "" : objArticle.DataItem.chvAuthor;

                        if (row["txtExpand"] != DBNull.Value)
                        {
                            string strXml = row["txtExpand"].ToString();
                            StringBuilder objbuilder = new StringBuilder();
                            try
                            {
                                if (!strXml.Equals(""))
                                {
                                    XmlDocument strXML = new XmlDocument();
                                    strXML.LoadXml(strXml);
                                    XmlNode objExtenRootNode = strXML.SelectSingleNode("/datamodel");
                                    if (objExtenRootNode != null)
                                    {
                                        if (objExtenRootNode.HasChildNodes)
                                        {
                                            XmlNodeList objXMLNOdeList = objExtenRootNode.ChildNodes;
                                            foreach (XmlNode objNodeTemp in objXMLNOdeList)
                                            {
                                                if (objNodeTemp.Attributes["type"] != null)
                                                {
                                                    string strType = objNodeTemp.Attributes["type"].Value;
                                                    if (File.Exists(Server.MapPath("~/") + "ServerPages\\ExpandEditor\\" + strType + "\\template.htm"))
                                                    {
                                                        using (FileStream fs = new FileStream(Server.MapPath("~/") + "ServerPages\\ExpandEditor\\" + strType + "\\template.htm", FileMode.Open, FileAccess.Read, FileShare.Read))
                                                        {
                                                            StreamReader sr = new StreamReader(fs);
                                                            string temp = sr.ReadToEnd();
                                                            temp = temp.Replace("$TitlePosition$", objNodeTemp.Attributes["description"].Value).Replace("$NamePosition$", objNodeTemp.Name).Replace("$IDPosition$", objNodeTemp.Name).Replace("$CurrentValue$", objNodeTemp.InnerText);
                                                            objbuilder.Append(temp);
                                                            sr.Close();
                                                        }
                                                    }
                                                    else
                                                    {
                                                        Alert(string.Format("扩展配置异常:栏目{0}中的节点ID为{1}的type属性为{2}所对应的文件不存在", State.intSTID, objNodeTemp.Name, strType));
                                                        return;
                                                    }
                                                }
                                                else
                                                {
                                                    Alert(string.Format("扩展配置异常:栏目{0}中的节点ID为{1}的type属性不存在", State.intSTID, objNodeTemp.Name));
                                                    return;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Alert(ex.Message.Replace("\r\n", ""));
                            }

                            LitExtention.Text = objbuilder.ToString();
                        }
                    }
                    objFactory.Dispose();
                    objArtSecRelationFactory.Dispose();
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
        }

        /// <summary>
        /// 显示自定义扩展
        /// </summary>
        /// <param name="strXml"></param>
        /// <returns></returns>
        protected string ShowExtension(string strXml)
        {
            StringBuilder objbuilder = new StringBuilder();
            try
            {
                if (!strXml.Trim().Equals(""))
                {
                    XmlDocument strXML = new XmlDocument();
                    strXML.LoadXml(strXml);
                    XmlNode objExtenRootNode = strXML.SelectSingleNode("/datamodel");
                    if (objExtenRootNode != null)
                    {
                        if (objExtenRootNode.HasChildNodes)
                        {
                            XmlNodeList objXMLNOdeList = objExtenRootNode.ChildNodes;
                            foreach (XmlNode objNodeTemp in objXMLNOdeList)
                            {
                                if (objNodeTemp.Attributes["type"] != null)
                                {
                                    string strType = objNodeTemp.Attributes["type"].Value;
                                    if (File.Exists(Server.MapPath("~/") + "ServerPages\\ExpandEditor\\" + strType + "\\template.htm"))
                                    {
                                        using (FileStream fs = new FileStream(Server.MapPath("~/") + "ServerPages\\ExpandEditor\\" + strType + "\\template.htm", FileMode.Open, FileAccess.Read, FileShare.Read))
                                        {
                                            StreamReader sr = new StreamReader(fs);
                                            string temp = sr.ReadToEnd();
                                            temp = temp.Replace("$TitlePosition$", objNodeTemp.Attributes["description"].Value).Replace("$NamePosition$", objNodeTemp.Name).Replace("$IDPosition$", objNodeTemp.Name).Replace("$CurrentValue$", "");
                                            objbuilder.Append(temp);
                                            sr.Close();
                                        }
                                    }
                                    else
                                    {
                                        Alert(string.Format("扩展配置异常:栏目{0}中的节点ID为{1}的type属性为{2}所对应的文件不存在", State.intSTID, objNodeTemp.Name, strType));
                                        return "";
                                    }
                                }
                                else
                                {
                                    Alert(string.Format("扩展配置异常:栏目{0}中的节点ID为{1}的type属性不存在", State.intSTID, objNodeTemp.Name));
                                    return "";
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message.Replace("\r\n", ""));
            }
            return objbuilder.ToString();
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }
            if (!ParamAuth.IsDateTime(SqlCommon.SqlStringFilter(this.txtIssueTime.Value)))
            {
                Alert("请输入正确的发布时间格式");
                bolIsValidate = false;
            }

            if (!ParamAuth.IsNumber(SqlCommon.SqlStringFilter(this.txtOrder.Value)))
            {
                Alert("排序请输入数字");
                bolIsValidate = false;
            }

            if (this.TextReadCount.Value.Trim() != "")
            {
                if (!ParamAuth.IsNumber(SqlCommon.SqlStringFilter(this.TextReadCount.Value.Trim())))
                {
                    Alert("阅读次数请输入数字");
                    bolIsValidate = false;
                }
            }
            return bolIsValidate;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            bool bolIssueResult=false;
            try
            {
                if (!ValidateValues())
                {
                    ExpandReset();
                    return;
                }
                using (DBManager objDBManager = new DBManager())
                {
                    objDBManager.BeginTransaction();
                    try
                    {
                        //关联表
                        ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory(objDBManager);
                        BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.Creator(Convert.ToInt32(Request["Id"].ToString().Trim()));
                        objArtSecRelation.DataItem.blnIssue = (int)Utility.ArticleState.NoIssue;
                        objArtSecRelation.DataItem.intState = (int)Utility.ArticleState.NoIssue;
                        objArtSecRelation.DataItem.intType = (int)Utility.ArticleType.Article;
                        //文章排序
                        objArtSecRelation.DataItem.intSort = int.Parse(this.txtOrder.Value);
                        //阅读次数
                        if (ParamAuth.IsNumber(this.TextReadCount.Value.Trim()))
                        {
                            objArtSecRelation.DataItem.intAccessCount = Convert.ToInt32(this.TextReadCount.Value.Trim());
                        }
                        //文章位置状态
                        objArtSecRelation.DataItem.intVerify = CkboxPublish.Checked == true ? 1 : 0;
                        objArtSecRelation.DataItem.intGlobalRec = CkboxIsGlobalRec.Checked == true ? 1 : 0;
                        objArtSecRelation.DataItem.intSectionRec = CkboxIsPartRec.Checked == true ? 1 : 0;
                        objArtSecRelation.DataItem.intGlobalTop = CkboxIsGlobalTop.Checked == true ? 1 : 0;
                        objArtSecRelation.DataItem.intSectionTop = CkboxIsPartTop.Checked == true ? 1 : 0;
                        objArtSecRelation.DataItem.intScroll = CkboxIsScroll.Checked == true ? 1 : 0;
                        objArtSecRelation.Update();

                        objArtSecRelation = objAsFactory.CreatorBySql(string.Format("  intArticleId={0} and intSectionID={1}", objArtSecRelation.DataItem.intArticleID, State.intSTID));
                        for (int i = 0; i < objArtSecRelation.Count; i++)
                        {
                            objArtSecRelation.DataItem.blnIssue = (int)Utility.ArticleState.NoIssue;
                            objArtSecRelation.DataItem.intState = (int)Utility.ArticleState.NoIssue;
                            objArtSecRelation.SelectRow(i + 1);
                        }
                        objArtSecRelation.Update();

                        ArticleFactory objFactory = new ArticleFactory(objDBManager);
                        BusinessLogic.BusinessRules.Article objArticle = objFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                        //文章表
                        objArticle.DataItem.chvKeyWords = this.txtkeyWord1.Value + "," + this.txtkeyWord2.Value + "," + this.txtkeyWord3.Value + "," + this.txtkeyWord4.Value;
                        objArticle.DataItem.chvSummary = this.txtSummary.Value.Replace("\"/news/" + State.LoginUser.VartualPath + "/Pcfiles/", "\"../Pcfiles/");
                        objArticle.DataItem.txtContent = this.txtContent.Value.Replace("\"/news/" + State.LoginUser.VartualPath + "/Pcfiles/", "\"../Pcfiles/");
                        if (Imghidden.Value.Length != 0)
                        {
                            objArticle.DataItem.chvMainMiniPic = Imghidden.Value;
                            objArticle.DataItem.chvMainPic = Imghidden.Value.Replace("mini", "");
                        }
                        objArticle.DataItem.dtmInTime = Convert.ToDateTime(this.txtIssueTime.Value);
                        objArticle.DataItem.chvTitle = this.txtTitle.Value;
                        objArticle.DataItem.chvSubTitle = this.txtSecondTitle.Value;
                        objArticle.DataItem.chvArticleInMan = this.txtAuthor.Value;
                        var inttxtdescriptionLength = this.txtdescription.Value.Length;
                        var intArticleDescriptionMaxLength = AppConfig.GetArticleDescriptionMaxLength();
                        if (inttxtdescriptionLength > intArticleDescriptionMaxLength)
                        {
                            objArticle.DataItem.chvDescription = this.txtdescription.Value.Remove(intArticleDescriptionMaxLength);
                        }
                        else
                        {
                            objArticle.DataItem.chvDescription = this.txtdescription.Value;
                        }
                        objArticle.DataItem.chvRedirectUrl = this.TextRedirectUrl.Value.Trim();
                        objArticle.DataItem.chvVedioPath = this.txtvedio.Value;
                        objArticle.DataItem.chvFilePath = this.txtfile.Value;
                        objArticle.DataItem.chvArticleSource = this.TextSource.Value;
                        objArticle.DataItem.chvAuthor = this.TextAuthor.Value;
                        objArticle.DataItem.txtSeoTitle = this.txtseotitle.Value.Trim().Length == 0 ? "    " : this.txtseotitle.Value;
                        objArticle.DataItem.txtSeoKeywords = this.txtseokeywords.Value.Trim().Length == 0 ? "    " : this.txtseokeywords.Value;
                        objArticle.DataItem.txtSeodescription = this.txtseodescription.Value.Trim().Length == 0 ? "    " : this.txtseodescription.Value;

                        SectionsFactory objSectionsFactory = new SectionsFactory(objDBManager);
                        BusinessLogic.BusinessRules.Sections objSections = objSectionsFactory.Creator(State.intSTID);
                        if (objSections != null)
                        {
                            DataTable tblTmp = objSections.Data.Tables[0];
                            DataRow row = tblTmp.Rows[0];
                            if (row["chvDataModel"] != DBNull.Value)
                            {
                                StringBuilder objbuilder = new StringBuilder();
                                try
                                {
                                    if (!objSections.DataItem.chvDataModel.Equals(""))
                                    {
                                        XmlDocument strXML = new XmlDocument();
                                        strXML.LoadXml(objSections.DataItem.chvDataModel);
                                        XmlNode objExtenRootNode = strXML.SelectSingleNode("/datamodel");
                                        if (objExtenRootNode != null)
                                        {
                                            if (objExtenRootNode.HasChildNodes)
                                            {
                                                XmlNodeList objXMLNOdeList = objExtenRootNode.ChildNodes;
                                                foreach (XmlNode objNodeTemp in objXMLNOdeList)
                                                {

                                                    if (Request[objNodeTemp.Name] != null)
                                                    {
                                                        string strFormValue = Request[objNodeTemp.Name];
                                                        XmlCDataSection objXmlCDataSection = strXML.CreateCDataSection(strFormValue);
                                                        objNodeTemp.AppendChild(objXmlCDataSection);
                                                    }
                                                }
                                            }
                                        }
                                        objArticle.DataItem.txtExpand = strXML.OuterXml;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Alert(ex.Message.Replace("\r\n", ""));
                                    return;
                                }
                            }
                        }
                        objArticle.Update();

                        //重置栏目相关均为未发布状态
                        objSections = objSectionsFactory.CreatorBySql(" intSectionID=" + State.intSTID + " or intReferSectionID=" + State.intSTID);
                        SectionsFactory objSFactory = new SectionsFactory(objDBManager);

                        if (objSections != null)
                        {
                            BusinessLogic.BusinessRules.Sections objSectionsmain = objSFactory.CreatorBySql(" intParentId=0 ");
                            if (objSectionsmain != null)
                            {
                                for (int i = 0; i < objSectionsmain.Count; i++)
                                {
                                    objSectionsmain.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                                    objSectionsmain.SelectRow(i + 1);
                                }
                                objSectionsmain.Update();
                            }
                            //提交
                            objDBManager.Commit();

                            for (int i = 0; i < objSections.Count; i++)
                            {
                                //重置栏目相关均为未发布状态
                                FindparentSection(objSections.DataItem.intSectionID);
                                bolIssueResult = CreateSavePage(State.intWSID, objArtSecRelation.DataItem.intSecArtID, objSections.DataItem.intSectionID);
                                objSections.SelectRow(i + 1);
                            }
                        }

                        AlertAndRedirect("修改成功" + (bolIssueResult == true ? ",已经自动发布" : "") + "！", "../Section/List.aspx?Section=" + Request["Section"].ToString(), false);
                    }
                    catch (Exception ex)
                    {
                        Alert(ex.Message.ToString().Replace("\r\n", " "));
                        objDBManager.Rollback();
                        objDBManager.Dispose();
                        ExpandReset();
                    }
                    finally
                    {
                        objDBManager.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
                //Alert(ex.ToString());
                return;
            }
        }

        private void FindparentSection(int intSectionId)
        {
            using (SectionsFactory objSFactory = new SectionsFactory())
            {
                BusinessLogic.BusinessRules.Sections objSections = objSFactory.Creator(intSectionId);
                if (objSections != null)
                {
                    objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                    objSections.Update();
                    objSections = objSFactory.CreatorBySql(" intParentId=" + objSections.DataItem.intParentID + " ");
                    if (objSections != null)
                    {
                        objSections.DataItem.intIssueState = (int)Utility.STIssueState.NoIssue;
                        objSections.Update();
                        FindparentSection(objSections.DataItem.intParentID);
                    }
                }
            }
        }
    }
}